package httpCtrler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"
	st "kjjcgroup_server/model"
	sv "kjjcgroup_server/service"
	"net/http"
	"strconv"
	"time"
)

func GuestbookRouter(router *gin.Engine, routerAuth *gin.RouterGroup) {
	router.GET("/guestbook", guestbook)
	LogMap["/guestbook"] = "查询留言"
	routerAuth.POST("/guestbookAdd", guestbookAdd)
	LogMap["/guestbookAdd"] = "添加留言"
	routerAuth.POST("/guestbookEdit", guestbookEdit)
	LogMap["/guestbookEdit"] = "编辑留言"
	routerAuth.DELETE("/guestbookDel", guestbookDel)
	LogMap["/guestbookDel"] = "删除留言"
}

func guestbook(c *gin.Context) {
	GuestbookId := c.Query("GuestbookId")
	mysql, err := sv.GetEngine()
	if err != nil {
		c.JSON(409, &MsgReturn{Msg: "Open DataBase Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {
			return
		}
	}(mysql)
	mysql.ShowSQL(true)

	var collArr []*st.GuestbookMod
	if GuestbookId != "" {
		err = mysql.Where("guestbook_id=?", GuestbookId).Find(&collArr)
	} else {
		err = mysql.Find(&collArr)
	}
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: collArr})
}

func guestbookAdd(c *gin.Context) {
	resDt := &st.GuestbookMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt)

	mysql, err := sv.GetEngine()
	//	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	mysql.ShowSQL(true)

	err = sess.Begin()
	if err != nil {
		return
	}
	t := time.Now()
	t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
	fmt.Println(t)
	_, err = sess.Cols("guestbook_user", "guestbook_name", "guestbook_phone", "guestbook_mail", "guestbook_des", "guestbook_pro", "guestbook_company", "guestbook_img", "create_time", "update_time").InsertOne(&st.GuestbookMod{GuestbookUser: resDt.GuestbookUser, GuestbookName: resDt.GuestbookName, GuestbookPhone: resDt.GuestbookPhone, GuestbookMail: resDt.GuestbookMail, GuestbookDes: resDt.GuestbookDes, GuestbookPro: resDt.GuestbookPro, GuestbookCompany: resDt.GuestbookCompany, GuestbookImg: resDt.GuestbookImg, CreateTime: t, UpdateTime: t})
	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "添加" + resDt.GuestbookName + "成功!"})
}

func guestbookEdit(c *gin.Context) {
	resDt := &st.GuestbookMod{}
	err := c.BindJSON(resDt)
	if err != nil {
		return
	}
	fmt.Printf("JhVar %+v \r\n", resDt.GuestbookRead)

	mysql, err := sv.GetEngine()
	mysql.ShowSQL(true)
	if err != nil {
		c.JSON(409, "")
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	sess := mysql.NewSession()
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}
	_, err = sess.Where("guestbook_id=?", resDt.GuestbookId).Update(&st.GuestbookMod{GuestbookUser: resDt.GuestbookUser, GuestbookName: resDt.GuestbookName, GuestbookPhone: resDt.GuestbookPhone, GuestbookMail: resDt.GuestbookMail, GuestbookDes: resDt.GuestbookDes, GuestbookPro: resDt.GuestbookPro, GuestbookCompany: resDt.GuestbookCompany, GuestbookImg: resDt.GuestbookImg})
	_, err = sess.Where("guestbook_id=?", resDt.GuestbookId).Cols("guestbook_read").Update(&st.GuestbookMod{GuestbookRead: resDt.GuestbookRead})

	if err != nil {
		err := sess.Rollback()
		if err != nil {
			return
		}
	} else {
		err = sess.Commit()
	}
	if err != nil {
		c.JSON(409, err.Error())
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + strconv.Itoa(resDt.GuestbookId) + " 编辑成功!"})
}

func guestbookDel(c *gin.Context) {
	GuestbookId := c.Query("GuestbookId")
	mysql, err := sv.GetEngBom()
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	defer func(mysql *xorm.Engine) {
		err := mysql.Close()
		if err != nil {

		}
	}(mysql)
	mysql.ShowSQL(true)
	_, err = mysql.Where("guestbook_id=?", GuestbookId).Delete(&st.GuestbookMod{})
	if err != nil {
		fmt.Println(err)
		c.JSON(409, &MsgReturn{Msg: "Failed", Data: nil})
		return
	}
	c.JSON(http.StatusOK, &MsgReturn{Msg: "OK", Data: "id:" + GuestbookId + " 删除成功!"})
}
